package experiment9;

import java.io.*;

public class No_5 {
    public static void main(String[] args) throws Exception {
        BitOutputStream output = new BitOutputStream(new File("test.dat"));
        output.writeBit("010000100100001001101");
        output.close();
        System.out.println("Successfully finished");
    }


    public static class BitOutputStream {
        private FileOutputStream output;
        private int value;
        private int count = 0;

        public BitOutputStream(File file) throws IOException {
            output = new FileOutputStream(file);
        }

        public void writeBit(char bit) throws IOException {
            count++;
            value = value << 1;

            // The bits are all zeros except the last one
            int mask = 1;
            if (bit == '1')
                value = value | mask;

            if (count == 8) {
                output.write(value);
                count = 0;
            }
        }

        public void writeBit(String bitString) throws IOException {
            for (int i = 0; i < bitString.length(); i++)
                writeBit(bitString.charAt(i));
        }

        public void close() throws IOException {
            if (count > 0) {
                value = value << (8 - count);
                output.write(value);
            }

            output.close();
        }
    }

}